package com.sc.service.promotion;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sc.dal.dto.promotion.PromotionDetailDto;
import com.sc.dal.dto.promotion.PromotionGoodsSkuDto;
import com.sc.dal.mapper.promotion.PromotionDetailMapper;
import com.sc.dal.mapper.promotion.PromotionGoodsSkuMapper;
import com.sc.dal.mapper.promotion.PromotionMapper;
import com.sc.dal.mapper.promotion.PromotionOrderRecordMapper;
import com.sc.dal.model.promotion.Promotion;
import com.sc.dal.model.promotion.PromotionOrderRecord;
import com.sc.service.BaseServiceImpl;

@Service
public class PromotionService extends  BaseServiceImpl<Promotion>{
	@Autowired
	private PromotionMapper promotionMapper;
	
	@Autowired
	private PromotionDetailMapper promotionDetailMapper;
	
	@Autowired
	private PromotionGoodsSkuMapper promotionGoodsSkuMapper;
	
	@Autowired
	private PromotionOrderRecordMapper promotionOrderRecordMapper;

	// 查询列表
	public List<Promotion> selectList(Map<String, Object> record) {
		return promotionMapper.selectList(record);
	}

	// 查询详情
	public Promotion selectByPrimaryKey(Integer id) {
		return promotionMapper.selectByPrimaryKey(id);
	}

	// 添加促销类型
	public int insert(Promotion record) {
		return promotionMapper.insert(record);
	}

	public int insertSelective(Promotion record) {
		return promotionMapper.insertSelective(record);
	}

	// 删除促销类型
	public int deleteByPrimaryKey(Integer id) {
		return promotionMapper.deleteByPrimaryKey(id);
	}

	// 编辑促销类型

	public int updateByPrimaryKeySelective(Promotion record) {
		return promotionMapper.updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(Promotion record) {
		return promotionMapper.updateByPrimaryKey(record);
	}
	
	//促销明细
	public List<Map<String,Object>>  selectByParam(Map<String,Object> map){
		return promotionDetailMapper.selectByParam(map);
	}
	
	//添加促销明细
	public int insertPromotionDetail(PromotionDetailDto dto){
		return promotionDetailMapper.insertSelective(dto);
	}
	// 删除促销详细
	public int deleteProDetailByPrimaryKey(Integer id) {
		return promotionDetailMapper.deleteByPrimaryKey(id);
	}
	
	// 查询详情
	public PromotionDetailDto selectProDetailByPrimaryKey(Integer id) {
		return promotionDetailMapper.selectProDetailByPrimaryKey(id);
	}

	public int updateProDetail(PromotionDetailDto dto){
		return promotionDetailMapper.updateProDetail(dto);
	}
	
    //添加商品赠品
	public int insertPromotionGoodsSku(PromotionGoodsSkuDto dto) {
		return promotionGoodsSkuMapper.insertPromotionGoodsSku(dto);
	}
	
	//获取商品参与的促销活动
	public List<PromotionGoodsSkuDto> getGoodsProInfo(Integer goods_id){
		return promotionGoodsSkuMapper.getGoodsProInfo(goods_id);
		
	}
	
	//获取促销详细列表
	public List<PromotionDetailDto> selectProDetailByProId(Integer pro_id){
		return promotionDetailMapper.selectProDetailByProId(pro_id);
	}
	
	//添加订单促销记录
	public int insertOrderProRecord(PromotionOrderRecord record){
		return promotionOrderRecordMapper.insertSelective(record);
	}
	
	/***
	 * 根据商品ID查询活动信息
	 * @param map
	 * @return
	 */
	public List<String> selectGoodsbyPromotion(Map<String, Object> map){
		return promotionMapper.selectGoodsbyPromotion(map);
	}
	
	
	/***
	 * 根据商品ID查询优惠活动信息
	 * @param map
	 * @return
	 */
	public String getGoodsByPromtion(Map<String, Object> map){
		List<String> proList = this.selectGoodsbyPromotion(map);
		String proTitle = "";
		for (int i = 0; i < proList.size(); i++) {
			//0:订单满减 1:商品满赠 2:商品满减 3:商品打折',
			String proType = proList.get(i);
			if(proType!=null && "1".equals(proType)){
				proTitle+="减"+"、";
			}
			if(proType!=null && "2".equals(proType)){
				proTitle+="赠"+"、";
			}
			if(proType!=null && "3".equals(proType)){
				proTitle+="折";
			}
		}
		if(proTitle.endsWith("、")){
			proTitle = proTitle.substring(0, proTitle.length()-1);
		}
		return proTitle;
	}
}
